<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-Hans">
<head><meta name="generator" content="Hexo 3.9.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-hemisu.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-hemisu.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-hemisu.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="读书笔记,js高程,">










<meta name="description" content="[TOC] 本章内容 理解Selectors API 使用HTML5 DOM拓展 了解专有的DOM拓展">
<meta name="keywords" content="读书笔记,js高程">
<meta property="og:type" content="article">
<meta property="og:title" content="js高程读书笔记 第十一章 DOM拓展">
<meta property="og:url" content="http://www.hemisu.com/2017/08/13/298/index.html">
<meta property="og:site_name" content="何米酥`s Blog">
<meta property="og:description" content="[TOC] 本章内容 理解Selectors API 使用HTML5 DOM拓展 了解专有的DOM拓展">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2017-08-13T19:38:26.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="js高程读书笔记 第十一章 DOM拓展">
<meta name="twitter:description" content="[TOC] 本章内容 理解Selectors API 使用HTML5 DOM拓展 了解专有的DOM拓展">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Gemini',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":true,"scrollpercent":true,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://www.hemisu.com/2017/08/13/298/">





  <title>js高程读书笔记 第十一章 DOM拓展 | 何米酥`s Blog</title>
  








  <!-- Hotjar Tracking Code for www.hemisu.com -->
  <script>
      (function(h,o,t,j,a,r){
          h.hj=h.hj||function(){(h.hj.q=h.hj.q||[]).push(arguments)};
          h._hjSettings={hjid:1933736,hjsv:6};
          a=o.getElementsByTagName('head')[0];
          r=o.createElement('script');r.async=1;
          r.src=t+h._hjSettings.hjid+j+h._hjSettings.hjsv;
          a.appendChild(r);
      })(window,document,'https://static.hotjar.com/c/hotjar-','.js?sv=');
  </script>
</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">何米酥`s Blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">EFE</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br>
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br>
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br>
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br>
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br>
            
            归档
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://www.hemisu.com/2017/08/13/298/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="何米酥">
      <meta itemprop="description" content>
      <meta itemprop="image" content="/images/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="何米酥`s Blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">js高程读书笔记 第十一章 DOM拓展</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-08-13T19:25:00+00:00">
                2017-08-13
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/笔记/" itemprop="url" rel="index">
                    <span itemprop="name">笔记</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>[TOC]</p>
<h1 id="本章内容"><a href="#本章内容" class="headerlink" title="本章内容"></a>本章内容</h1><ul>
<li>理解Selectors API</li>
<li>使用HTML5 DOM拓展</li>
<li>了解专有的DOM拓展</li>
</ul>
<a id="more"></a>
<h1 id="选择符API"><a href="#选择符API" class="headerlink" title="选择符API"></a>选择符API</h1><p>querySelector()和querySelectorAll()</p>
<p>其中querySelectorAll()放回的是带有所有属性和方法的NodeList，而其底层类似于一组元素的快照，而非不断对文档进行搜索的动态查询。</p>
<p>machesSelector()方法</p>
<p>如果调用的元素与该选择符匹配，返回true<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">matchesSelector</span>(<span class="params">elememt, selector</span>)</span>&#123;</span><br><span class="line">    <span class="keyword">if</span>(element.matchesSelector)&#123;</span><br><span class="line">        <span class="keyword">return</span> element.matchesSelector(selector);</span><br><span class="line">    &#125;<span class="keyword">else</span> <span class="keyword">if</span>(element.msMatchesSelector)&#123;</span><br><span class="line">        <span class="keyword">return</span> element.msMatchesSelector(selector);</span><br><span class="line">    &#125;<span class="keyword">else</span> <span class="keyword">if</span>(element.mozMatchesSelector)&#123;</span><br><span class="line">        <span class="keyword">return</span> element.mozMatchesSelector(selector);</span><br><span class="line">    &#125;<span class="keyword">else</span> <span class="keyword">if</span>(element.webkitMatchesSelector)&#123;</span><br><span class="line">        <span class="keyword">return</span> element.webkitMatchesSelector(selector);</span><br><span class="line">    &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">        <span class="keyword">throw</span> <span class="keyword">new</span> <span class="built_in">Error</span>(<span class="string">"Not supported."</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span>(matchesSelector(<span class="built_in">document</span>.body, <span class="string">'body.page1'</span>))&#123;</span><br><span class="line">    <span class="comment">//操作</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<h1 id="元素遍历"><a href="#元素遍历" class="headerlink" title="元素遍历"></a>元素遍历</h1><p>IE9以及之前，元素间的空格会被认成文本节点。需要判断nodeType == 1来筛选元素。</p>
<p>新元素遍历规范Element Traversal规范定义了一组属性</p>
<ul>
<li>childElementCount</li>
<li>firstElementChild 对应firstChild元素版</li>
<li>lastElementChild 对应lastChild元素版</li>
<li>previousElementSibling 对应previousSibling元素版</li>
<li>nextElementSibling 对应nextSibling元素版</li>
</ul>
<h1 id="HTML5"><a href="#HTML5" class="headerlink" title="HTML5"></a>HTML5</h1><h2 id="与类相关的扩充"><a href="#与类相关的扩充" class="headerlink" title="与类相关的扩充"></a>与类相关的扩充</h2><p>getElementsByClassName()方法接收类名，返回NodeList<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//获取所有类中包含“username”和current的元素</span></span><br><span class="line"><span class="keyword">var</span> allCurrentUsernames = <span class="built_in">document</span>.getElementsByClassName(<span class="string">"username current"</span>);</span><br><span class="line"><span class="comment">//取得ID为myDiv元素中带有类名selected的所有元素</span></span><br><span class="line"><span class="keyword">var</span> selected = <span class="built_in">document</span>.getElementById(<span class="string">"myDIv"</span>).getElementsByClassName(<span class="string">"selected"</span>);</span><br></pre></td></tr></table></figure></p>
<h2 id="classList属性"><a href="#classList属性" class="headerlink" title="classList属性"></a>classList属性</h2><p>在操作类名时，className属性添加、删除和替换类名。<br><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">"bd user disabled"</span>&gt;</span>...<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br></pre></td></tr></table></figure></p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//删除user类</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//取得类名字符串并拆分成数组</span></span><br><span class="line"><span class="keyword">var</span> classNames = div.className.split(<span class="regexp">/\s+/</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">//找到要删的类名</span></span><br><span class="line"><span class="keyword">var</span> pos = <span class="number">-1</span>,</span><br><span class="line">    i,</span><br><span class="line">    len;</span><br><span class="line"><span class="keyword">for</span>(i = <span class="number">0</span>, len = classNames.length; i &lt; len; i++)&#123;</span><br><span class="line">    <span class="keyword">if</span>(className[i] == <span class="string">"user"</span>)&#123;</span><br><span class="line">        pos = i;</span><br><span class="line">        <span class="keyword">break</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//删除类名</span></span><br><span class="line">classNames.splice(i,<span class="number">1</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">//把剩下的类名拼成字符串并重新设置</span></span><br><span class="line">div.className = className.join(<span class="string">" "</span>);</span><br></pre></td></tr></table></figure>
<p>HTML5 新增一种操作类名的方式，所有的元素添加了classList属性，是新集合类型DOMTokenList的实例。</p>
<ul>
<li>add(value) 将给定字符串添加到列表中</li>
<li>contains(Value) 存在值返回true</li>
<li>remove(value) 删除给定字符串</li>
<li>toggle(value) 如果列表中已经存在给定的值，删除它；没有给定的值，添加它。</li>
</ul>
<p>前面这么多行用一行就可以代替了<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">div.classList.remove(<span class="string">"user"</span>);</span><br></pre></td></tr></table></figure></p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//删除"disabled"类</span></span><br><span class="line">div.classList.remove(<span class="string">"disabled"</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">//添加"添加current"类</span></span><br><span class="line">div.classList.toggle(<span class="string">"user"</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">//确定元素中是否包含既定的类名</span></span><br><span class="line"><span class="keyword">if</span>(div.classList.contain(<span class="string">"bd"</span>) &amp;&amp; !div.classList.contains(<span class="string">"disabled"</span>))&#123;</span><br><span class="line">    <span class="comment">//执行操作</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//迭代类名</span></span><br><span class="line"><span class="keyword">for</span>(<span class="keyword">var</span> i = <span class="number">0</span>,len = div.classList.length;i &lt; len; i++)&#123;</span><br><span class="line">    doSomething(div.classList[i]);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="焦点管理"><a href="#焦点管理" class="headerlink" title="焦点管理"></a>焦点管理</h2><p>元素获得焦点的方式有页面加载、用户输入（通常tab键）和代码中调用focus()方法。</p>
<p>默认情况下，文档刚刚加载完成时，document.activeElement中保存的是document.body元素。加载期间其值为null。</p>
<p>新增document.hasFocus()方法用来确定文档是否获得了焦点。</p>
<h2 id="HTMLDocument的变化"><a href="#HTMLDocument的变化" class="headerlink" title="HTMLDocument的变化"></a>HTMLDocument的变化</h2><h3 id="readyState属性"><a href="#readyState属性" class="headerlink" title="readyState属性"></a>readyState属性</h3><ul>
<li>loading 正在加载文档</li>
<li>complete 已经加载完文档</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span>(<span class="built_in">document</span>.readyState == <span class="string">"complete"</span>)&#123;</span><br><span class="line">    <span class="comment">//执行操作</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="兼容模式"><a href="#兼容模式" class="headerlink" title="兼容模式"></a>兼容模式</h3><p>标准\混杂<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span>(<span class="built_in">document</span>.compatMode == <span class="string">"CSS1Compat"</span>)&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">"标准模式standard"</span>)</span><br><span class="line">&#125;<span class="keyword">else</span> <span class="keyword">if</span>(<span class="built_in">document</span>.compatMode == <span class="string">"BackCompat"</span>)&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">"混合模式quirks"</span>)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<h3 id="head属性"><a href="#head属性" class="headerlink" title="head属性"></a>head属性</h3><p>引用文档<code>&lt;head&gt;</code>元素<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> head = <span class="built_in">document</span>.head || <span class="built_in">document</span>.getElementsByTagName(<span class="string">"head"</span>)[<span class="number">0</span>];</span><br></pre></td></tr></table></figure></p>
<h3 id="字符集属性"><a href="#字符集属性" class="headerlink" title="字符集属性"></a>字符集属性</h3><p>charset属性表示文档中实际使用的字符集。默认情况的值为“UTF-16”。可以通过<code>&lt;meta&gt;</code>元素，响应头部或直接设置charset属性修改这个值。<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">console</span>.log(<span class="built_in">document</span>.charset);<span class="comment">//"UTF-16"</span></span><br><span class="line"><span class="built_in">document</span>.charset = <span class="string">"UTF-8"</span>;</span><br></pre></td></tr></table></figure></p>
<p>另一个值是defaultCharset，表示根据默认浏览器及操作系统的设置。</p>
<h3 id="自定义数据属性"><a href="#自定义数据属性" class="headerlink" title="自定义数据属性"></a>自定义数据属性</h3><p>HTML5规定可以为元素添加非标准的属性，但要添加前缀data-，目的是为元素提供与渲染无关的信息，或者提供语义信息。<br><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">"myDiv"</span> <span class="attr">data-appId</span>=<span class="string">"12345"</span> <span class="attr">data-myname</span>=<span class="string">"Nicholas"</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br></pre></td></tr></table></figure></p>
<p>可以通过元素的dataset属性来访问自定义属性的值。dataset属性的值是DOMStringMap的一个实例，也就是一个名值对的映射。<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> div = <span class="built_in">document</span>.getElementById(<span class="string">"myDiv"</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> appId = div.dataset.appId;</span><br><span class="line"><span class="keyword">var</span> myname = div.dataset.myname;</span><br><span class="line"></span><br><span class="line"><span class="comment">//设置值</span></span><br><span class="line">div.dataset.appId = <span class="number">23456</span>;</span><br><span class="line">div.dataset.myname = <span class="string">"Nicholas"</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span>(div.dataset.myname)&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">"Hello, "</span>+ div.dataset.myname);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<h2 id="插入标记"><a href="#插入标记" class="headerlink" title="插入标记"></a>插入标记</h2><h3 id="innerHTML属性"><a href="#innerHTML属性" class="headerlink" title="innerHTML属性"></a>innerHTML属性</h3><p>在写模式下，innerHTML的值会被解析为DOM子树，替换调用元素原来的所有子节点。</p>
<p>大多数浏览器中，通过innerHTML插入<code>&lt;script&gt;</code>元素并不会执行其中的脚本。IE8及更早版本是唯一能在这种情况下执行脚本的浏览器。需要满足一、为<code>&lt;script&gt;</code>指定defer属性，二、<code>&lt;script&gt;</code>元素必须位于“有作用域的元素”，也就是在页面中看得到的元素。</p>
<h3 id="outerHTML属性"><a href="#outerHTML属性" class="headerlink" title="outerHTML属性"></a>outerHTML属性</h3><p>outerHTML返回调用它的元素及所有子节点的HTML标签。</p>
<p>使用outerHTML属性设置值，会取代原来的元素。</p>
<h3 id="insertAdjacentHTML-方法"><a href="#insertAdjacentHTML-方法" class="headerlink" title="insertAdjacentHTML()方法"></a>insertAdjacentHTML()方法</h3><p>接收两个参数，插入位置和要插入的HTML文本。</p>
<ul>
<li>beforebegin 在当前元素之前插入一个紧邻的同辈元素</li>
<li>afterbegin 在当前元素之下插入一个新的子元素或者在第一个子元素之前再插入新的子元素</li>
<li>beforeend 在当前元素之下插入一个新的子元素或者在最后一个子元素之后再插入新的子元素</li>
<li>afterend 在当前元素之后插入一个紧邻的同辈元素。</li>
</ul>
<h3 id="内存与性能问题"><a href="#内存与性能问题" class="headerlink" title="内存与性能问题"></a>内存与性能问题</h3><p>在删除带有时间处理程序或者引用了其他JavaScript对象子树时，元素与事件处理程序之间的绑定关系在内存中并没有一并删除。在使用innerHTML、outerHTML和insertAdjacentHTML()方法时，最好先手工删除要被替换的元素的所有事件处理程序和JavaScript对象属性。</p>
<p>使用innerHTML与之前多次创建DOM节点再指定他们的关系相比，效率更高（解析器是浏览器级别的代码，通常是C++）。不过最好也能够将设置innerHTML或outerHTML的次数控制在合理的范围内。</p>
<h3 id="scrollIntoView-方法"><a href="#scrollIntoView-方法" class="headerlink" title="scrollIntoView()方法"></a>scrollIntoView()方法</h3><p>传入true为参数，或者不传入任何参数，窗口会滚动至让调用元素的顶部与视口顶部平齐。如果传入false作为参数，调用元素会尽可能全部出现在视口中。</p>
<h1 id="专有拓展"><a href="#专有拓展" class="headerlink" title="专有拓展"></a>专有拓展</h1><h2 id="文档模式"><a href="#文档模式" class="headerlink" title="文档模式"></a>文档模式</h2><h2 id="children属性"><a href="#children属性" class="headerlink" title="children属性"></a>children属性</h2><h2 id="contains-方法"><a href="#contains-方法" class="headerlink" title="contains()方法"></a>contains()方法</h2><p>调用这个方法的是祖先节点。这个方法接收一个参数，即要检测的后代节点。如果是后代节点则返回true。</p>
<p>DOM Level 3 compareDocumentPosition()也能确定节点间的关系。返回一个表示该关系的位掩码。</p>
<table>
<thead>
<tr>
<th>掩码</th>
<th>节点关系</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>无关</td>
</tr>
<tr>
<td>2</td>
<td>居前</td>
</tr>
<tr>
<td>4</td>
<td>局后</td>
</tr>
<tr>
<td>8</td>
<td>包含（给定的节点是参考节点的祖先）</td>
</tr>
<tr>
<td>16</td>
<td>被包含（给定节点是参考节点的后代）</td>
</tr>
</tbody>
</table>
<h2 id="插入文本"><a href="#插入文本" class="headerlink" title="插入文本"></a>插入文本</h2><p>innerText属性会过滤html标签</p>
<p>outerText属性将作用范围扩大到了包含调用它的节点之外。</p>
<h2 id="滚动"><a href="#滚动" class="headerlink" title="滚动"></a>滚动</h2><ul>
<li>scrollIntoViewIfNeeded(alignCenter) 如果不可见才滚动，参数设置true表示尽量将元素显示在视口中部</li>
<li>scrollByLines() 将元素的内容滚动指定行高</li>
<li>scrollByPages() 将元素的内容滚动指定页面高度</li>
</ul>
<h1 id="小结"><a href="#小结" class="headerlink" title="小结"></a>小结</h1><ul>
<li>Selector API 提供了 querySelector()和querySelectorAll()方法</li>
<li>Element Traversal 为DOM元素定义了额外的属性</li>
<li>HTML5 innerHTML，管理焦点，设置字符集、页面滚动等等</li>
</ul>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/读书笔记/" rel="tag"># 读书笔记</a>
          
            <a href="/tags/js高程/" rel="tag"># js高程</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2017/08/13/294/" rel="next" title="js高程读书笔记 第十章 DOM">
                <i class="fa fa-chevron-left"></i> js高程读书笔记 第十章 DOM
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2017/08/15/300/" rel="prev" title="js高程读书笔记 第十二章 DOM2和DOM3">
                js高程读书笔记 第十二章 DOM2和DOM3 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image" src="/images/avatar.jpg" alt="何米酥">
            
              <p class="site-author-name" itemprop="name">何米酥</p>
              <p class="site-description motion-element" itemprop="description">Just do...</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">202</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">8</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">78</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/hemisu" target="_blank" title="GitHub">
                      
                        <i class="fa fa-fw fa-github"></i>GitHub</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://www.zhihu.com/people/hemisu" target="_blank" title="知乎">
                      
                        <i class="fa fa-fw fa-globe"></i>知乎</a>
                  </span>
                
            </div>
          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#本章内容"><span class="nav-number">1.</span> <span class="nav-text">本章内容</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#选择符API"><span class="nav-number">2.</span> <span class="nav-text">选择符API</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#元素遍历"><span class="nav-number">3.</span> <span class="nav-text">元素遍历</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#HTML5"><span class="nav-number">4.</span> <span class="nav-text">HTML5</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#与类相关的扩充"><span class="nav-number">4.1.</span> <span class="nav-text">与类相关的扩充</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#classList属性"><span class="nav-number">4.2.</span> <span class="nav-text">classList属性</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#焦点管理"><span class="nav-number">4.3.</span> <span class="nav-text">焦点管理</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#HTMLDocument的变化"><span class="nav-number">4.4.</span> <span class="nav-text">HTMLDocument的变化</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#readyState属性"><span class="nav-number">4.4.1.</span> <span class="nav-text">readyState属性</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#兼容模式"><span class="nav-number">4.4.2.</span> <span class="nav-text">兼容模式</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#head属性"><span class="nav-number">4.4.3.</span> <span class="nav-text">head属性</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#字符集属性"><span class="nav-number">4.4.4.</span> <span class="nav-text">字符集属性</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#自定义数据属性"><span class="nav-number">4.4.5.</span> <span class="nav-text">自定义数据属性</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#插入标记"><span class="nav-number">4.5.</span> <span class="nav-text">插入标记</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#innerHTML属性"><span class="nav-number">4.5.1.</span> <span class="nav-text">innerHTML属性</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#outerHTML属性"><span class="nav-number">4.5.2.</span> <span class="nav-text">outerHTML属性</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#insertAdjacentHTML-方法"><span class="nav-number">4.5.3.</span> <span class="nav-text">insertAdjacentHTML()方法</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#内存与性能问题"><span class="nav-number">4.5.4.</span> <span class="nav-text">内存与性能问题</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#scrollIntoView-方法"><span class="nav-number">4.5.5.</span> <span class="nav-text">scrollIntoView()方法</span></a></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#专有拓展"><span class="nav-number">5.</span> <span class="nav-text">专有拓展</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#文档模式"><span class="nav-number">5.1.</span> <span class="nav-text">文档模式</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#children属性"><span class="nav-number">5.2.</span> <span class="nav-text">children属性</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#contains-方法"><span class="nav-number">5.3.</span> <span class="nav-text">contains()方法</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#插入文本"><span class="nav-number">5.4.</span> <span class="nav-text">插入文本</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#滚动"><span class="nav-number">5.5.</span> <span class="nav-text">滚动</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#小结"><span class="nav-number">6.</span> <span class="nav-text">小结</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      
        <div class="back-to-top">
          <i class="fa fa-arrow-up"></i>
          
            <span id="scrollpercent"><span>0</span>%</span>
          
        </div>
      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; 2015 &mdash; <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">何米酥</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Gemini</a> v5.1.4</div>




        







        
      </div>
    </footer>

    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
